//
//  ViewController.m
//  PaletteDemo
//
//  Created by 卢浩 on 16/4/26.
//  Copyright © 2016年 卢浩. All rights reserved.
//

#import "ViewController.h"
#import "TouchView.h"

@interface ViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>

@property (nonatomic,strong) TouchView *touchView;

@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;

@property (nonatomic,strong) NSMutableArray *lineWidthArr;

@property (nonatomic,strong) NSMutableArray *colorArr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"清空" style:(UIBarButtonItemStylePlain) target:self action:@selector(clearPalette)];

    //添加画板视图
    self.touchView = [[TouchView alloc]initWithFrame:CGRectMake(2, 66, self.view.frame.size.width - 4, self.view.frame.size.height - 200)];
    self.touchView.backgroundColor = [UIColor whiteColor];
    //设置画板边框
    self.touchView.layer.borderWidth = 2;
    self.touchView.layer.borderColor = [UIColor blackColor].CGColor;
    //开启用户交互
    self.touchView.userInteractionEnabled = YES;
    [self.view addSubview:self.touchView];
    
    //设置代理
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    
    //初始化数组
    self.lineWidthArr = [NSMutableArray array];
    self.colorArr = [NSMutableArray array];
    
    [self loadData];
}

//清空画板
- (void)clearPalette{
    [self.touchView.allPoints removeAllObjects];
    [self.touchView.points removeAllObjects];
    [self.touchView setNeedsDisplay];
}

//添加数据
- (void)loadData{
    for (int i = 1; i < 10; i++) {
        [self.lineWidthArr addObject:@(i)];
    }
    [self.colorArr addObjectsFromArray:@[@"黑色",@"红色",@"蓝色",@"黄色",@"绿色",@"灰色"]];
    [self.pickerView reloadAllComponents];
}

//设置有几列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 2;
}

//设置此列有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return self.lineWidthArr.count;
    }else{
        return self.colorArr.count;
    }
}

//设置每列每行的标题
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component == 0) {
        return [self.lineWidthArr[row] stringValue];
    }else{
        return self.colorArr[row];
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 40;
}

//选中哪一项的时候
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component == 0) {
        CGFloat lineWidth = [self.lineWidthArr[row] floatValue];
        self.touchView.lineWidth = lineWidth;
    }else{
        NSString *colorStr = self.colorArr[row];
        UIColor *color;
        if ([colorStr isEqualToString:@"黑色"]) {
            color = [UIColor blackColor];
        }else if ([colorStr isEqualToString:@"红色"]){
            color = [UIColor redColor];
        }else if ([colorStr isEqualToString:@"黄色"]){
            color = [UIColor yellowColor];
        }else if ([colorStr isEqualToString:@"绿色"]){
            color = [UIColor greenColor];
        }else if ([colorStr isEqualToString:@"蓝色"]){
            color = [UIColor blueColor];
        }else if ([colorStr isEqualToString:@"灰色"]){
            color = [UIColor grayColor];
        }
        self.touchView.color = color;
    }
    [self.touchView setNeedsDisplay];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
